<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: YanHilin
  Date: 2020/4/25
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
    <%@ include file="/page/comment/head.jsp"%>
    <base href="http://localhost:8080/onlineflower/">
    <style type="text/css">
        table,th,tr,td{
            text-align: center;
        }
        #inputCount{
            border: none;
            width: 30px;
        }
        .price{
            color: red;
            font-size: 18px;
        }
    </style>
</head>
<script src="static/js/jquery-3.4.1.js"></script>

<body>
<c:if test="${empty sessionScope.user}">
    <%@include file="/page/comment/head_nav.jsp"%>
</c:if>
<c:if test="${not empty sessionScope.user}">
    <%@include file="/page/user/head_nav.jsp"%>
</c:if>
<div>
    <c:if test="${empty sessionScope.cart.items}">
        <h1>购物车空空如也</h1>
        <h2><a href="page/user/index.jsp">去逛逛</a></h2>
    </c:if>
    <c:if test="${not empty sessionScope.cart.items}">
        <table class="table">
            <thead class="thead-light">
            <tr>
                <th scope="col">鲜花编号</th>
                <th scope="col">商铺名称</th>
                <th scope="col">鲜花名称</th>
                <th scope="col">数量/件</th>
                <th scope="col">单价/元</th>
                <th scope="col">小计/元</th>
                <th scope="col" colspan="2">操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${sessionScope.cart.items}" var="entry">
                <tr>
                    <td>${entry.value.id}</td>
                    <td>${entry.value.enterpriseid}</td>
                    <td scope="row">${entry.value.name}</td>
                    <td><input id="inputCount" class="updateCount" flowerid="${entry.value.id}" type="text" name="count" value="${entry.value.count}"></td>
                    <td>${entry.value.price}</td>
                    <td>${entry.value.totalPrice}</td>
                    <td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${entry.value.id}">删除</a></td>
                </tr>
            </c:forEach>
            <c:if test="${not empty sessionScope.cart.items}">
                <tr>
                    <td></td><td></td>
                    <td>  购物车中共有 <span class="price">${sessionScope.cart.totalCount}</span>件鲜花产品，
                        总金额<span class="price">${sessionScope.cart.totalPrice}</span>元</td>
                    <td></td>
                    <td><a class="clear" href="cartServlet?action=clear">清空购物车</a>
                        <a href="orderServlet?action=createOrder">下订单</a>
                    </td>

                </tr>
            </c:if>
            <c:if test="${empty sessionScope.cart.items}">
                <tr><td>快去购物把！！</td></tr>
            </c:if>
            </tbody>
        </table>
    </c:if>
</div>

</body>
<script>
    $(function () {
        $("a.deleteItem").click(function () {
            if( confirm("确定要删除"+$(this).parent().parent().find("td:first").text()+"吗？")){

            }else{
                return false;
            }
        });
        $("a.clear").click(function () {
            if( confirm("确定清空购物车吗？")){

            }else{
                return false;
            }
        });
        $(".updateCount").change(function () {
            var id=$(this).attr("flowerid");
            var count=this.value;
            if(confirm("确定将数量更改为"+count+"吗？")){
                location.href="http://localhost:8080/onlineflower/cartServlet?action=updateCount&count="+count+"&id="+id;
            }else{
                this.value=this.defaultValue;
            }
        });
    });
</script>
</html>
